package com.qkjia.springselltest.exdemo;

import org.springframework.shell.Availability;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellMethodAvailability;
//SpringShell提供了三种方式来限制命令是否可用.
/**
 * SpringShell声明的命令, 在默认情况下都是可用的. 但有时我们要实现命令依赖, 就是说要执行这条命令必须先执行依赖的命令,
 * 就像ftp命令, 需要先使用open 命令连接到ftp服务器之后, 才能执行其它命令. SpringShell
 * 中可借助于自定义命令是否可用方法的方式来实现命令依赖, 对此, SpringShell提供了三种方式来限制命令是否可用.
 * 但无论哪种方式都需要提供一个判断命令是否可用的方法.

 */
@ShellComponent//同Component
public class ZqkMyCommands2 {
//    . 校验是否可用方法
//    方法返回值类型为 Availability, 当不可用时需要设置提示信息
//    方法为无参方法
//    public Availability $methodName(){
//        String message = "Sorry! You are not connected";
//        return $boolen ? Availability.available() : Availability.unavailable(message);
//    }

//    2. 三种方式
//2.1 默认方法名方式
//    方法名必须为 “命令名Availability”, 一个方法只能限制一条命令
/*

    private boolean connected;

    @ShellMethod("Connect to the server.")
    public void connect(String user, String password) {
        connected = true;
    }

    @ShellMethod("Download the nuclear codes.")
    public String download() {
        return "execute downloading ...";
    }

    // 只能限制download 命令
    public Availability downloadAvailability(){
        String message = "Sorry! You are not connected";
        return connected ? Availability.available() : Availability.unavailable(message);
    }
*/
    /*测试
    shell:>download
    Command 'download' exists but is not currently available because Sorry! You are not connected
    Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.*/

//    2.2 注解修饰命令
//    声明命令时, 使用@ShellMethodAvailability 注解指定该命令是否可用的校验方法, 可指定多个校验方法

  /*  private boolean connected;

    @ShellMethod("Connect to the server.")
    public void connect(String user, String password) {
        connected = true;
    }

    @ShellMethod("Download the nuclear codes.")
    @ShellMethodAvailability({"checkAuth"})//注解指定该命令是否可用的校验方法, 可指定多个校验方法
    public String download() {
        return "execute downloading ...";
    }

    public Availability checkAuth() {
        String message = "Sorry! You are not connected";
        return connected ? Availability.available() : Availability.unavailable(message);
    }*/
    /*测试
    shell:>download
    Command 'download' exists but is not currently available because Sorry! You are not connected
    Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.*/

    //    2.3 定义校验方法时, 指定约束的方法
    //    定义校验方法时, 声明该校验方法约束的命令, 可声明多个命令.
    private boolean connected;

    @ShellMethod("Connect to the server.")
    public void connect(String user, String password) {
        connected = true;
    }

    @ShellMethod("Download the nuclear codes.")
    public String download() {
        return "execute downloading ...";
    }

    // 指定该方法限制的命令
    @ShellMethodAvailability({"download"})
    public Availability checkAuth() {
        String message = "Sorry! You are not connected";
        return connected ? Availability.available() : Availability.unavailable(message);
    }


/*    3. 测试
3.1 查看命令列表
    不可用命令, 会添加*号修饰

    shell:>help
    AVAILABLE COMMANDS

    Availability Commands
    connect: Connect to the server.
            * download: Download the nuclear codes.

            3.2 执行不可用命令
    执行不可用命令时, 会弹出提示信息. beacause 后就是自定义的错误提示信息

    shell:>download
    Command 'download' exists but is not currently available because Sorry! You are not connected
    Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.

        3.3 正确执行方式
# 先执行connect 命令
    shell:>connect root 123456

            # 执行connect命令之后, download命令可执行
    shell:>download
    execute downloading ...

            # 再次查看可用命令, 会发现download前已无*号
    shell:>help
    AVAILABLE COMMANDS

    Availability Commands
    connect: Connect to the server.
    download: Download the nuclear codes.*/

}
